﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using TankGameV1._0.Properties;

namespace TankGameV1._0
{
    class SingleObject
    {
        //用来创建全局唯一游戏对象
        private SingleObject() { }
        private static SingleObject singleObject = null;
        public static SingleObject GetObject()
        {
            if (singleObject == null)
            {
                singleObject = new SingleObject();
                return singleObject;
            }
            return singleObject;
        }
        public PlayerTank PT
        {
            get;
            set;
        }
        //创建一个泛型集合保存敌人坦克对象
        List<EnemyTank> enemyTanks = new List<EnemyTank>();
        List<PlayerBullet> playerBullets = new List<PlayerBullet>();
        List<EnemyBullet> enemyBullets = new List<EnemyBullet>();
        List<Boom> booms = new List<Boom>();
        List<Born> borns = new List<Born>();
        List<Equipment> equipments = new List<Equipment>();
        List<Wall> walls = new List<Wall>();
        public void AddGameObject(GameObject g)
        {
            if(g is PlayerTank)
            {
                PT = g as PlayerTank;//如果转换成功就返回相应对象，否者返回null
            }else if(g is EnemyTank)
            {
                enemyTanks.Add(g as EnemyTank);
            }else if(g is PlayerBullet)
            {
                playerBullets.Add(g as PlayerBullet);
            }else if(g is EnemyBullet)
            {
                enemyBullets.Add(g as EnemyBullet);
            }else if(g is Boom)
            {
                booms.Add(g as Boom);
            }else if(g is Born)
            {
                borns.Add(g as Born);
            }else if(g is Equipment)
            {
                equipments.Add(g as Equipment);
            }else if(g is Wall)
            {
                walls.Add(g as Wall);
            }
        }
        public void Draw(Graphics g)
        {
            //绘画玩家坦克
            PT.Draw(g);
            //绘制敌人坦克
            foreach(EnemyTank enemyTank in enemyTanks)
            {
                enemyTank.Draw(g);
            }
            foreach(PlayerBullet playerBullet in playerBullets)
            {
                playerBullet.Draw(g);
            }
            foreach(EnemyBullet enemyBullet in enemyBullets)
            {
                enemyBullet.Draw(g);
            }
            for(int i = 0; i < booms.Count; i++)
            {
                //此处不能用foreach
                booms[i].Draw(g);
            }
            for(int i = 0;i< borns.Count; i++)
            {
                borns[i].Draw(g);
            }
            foreach(Equipment equipment in equipments)
            {
                equipment.Draw(g);
            }
            foreach(Wall wall in walls)
            {
                wall.Draw(g);
            }
        }
        public void PZJC()
        {
            #region 玩家子弹打到敌人身上
            for (int i = 0; i < playerBullets.Count; i++)
            {
                for(int j = 0; j < enemyTanks.Count; j++)
                {
                    if (playerBullets[i].GetRectangle().IntersectsWith(enemyTanks[j].GetRectangle()))
                    {
                        enemyTanks[j].Life -= playerBullets[i].Power;                       
                        enemyTanks[j].IsOver();
                        //子弹打中敌人后就应该消失
                        playerBullets.Remove(playerBullets[i]);
                        break;
                    }
                }
            }
            #endregion
            #region 敌人子弹打到玩家身上
            for(int i = 0; i < enemyBullets.Count; i++)
            {
                if (enemyBullets[i].GetRectangle().IntersectsWith(PT.GetRectangle()))
                {
                    //此时玩家坦克不再无敌
                    PT.IsOver();
                    //若不删除子弹对象此处即为穿墙外挂
                    enemyBullets.Remove(enemyBullets[i]);
                }
            }
            #endregion
            #region 玩家吃到了装备
            for(int i = 0;i < equipments.Count; i++)
            {
                if (equipments[i].GetRectangle().IntersectsWith(PT.GetRectangle()))
                {
                    //使用装备效果
                    FireJudge(equipments[i].EquipmentType);
                    //移除装备图标
                    equipments.Remove(equipments[i]);
                    //播放吃到装备的音乐
                    SoundPlayer sp = new SoundPlayer(Resources.add);
                    sp.Play();
                }
            }
            #endregion
            #region 判断敌人和墙体是否碰撞
            for(int i = 0; i < walls.Count; i++)
            {
                for(int j = 0; j < enemyTanks.Count; j++)
                {
                    if (walls[i].GetRectangle().IntersectsWith(enemyTanks[j].GetRectangle()))
                    {
                        switch (enemyTanks[j].Dir)
                        {
                            case GameObject.Direction.Up:
                                enemyTanks[j].Y = walls[i].Y + walls[i].Height;
                                break;
                            case GameObject.Direction.Down:
                                enemyTanks[j].Y = walls[i].Y - walls[i].Height;
                                break;
                            case GameObject.Direction.Left:
                                enemyTanks[j].X = walls[i].X + walls[i].Width;
                                break;
                            case GameObject.Direction.Right:
                                enemyTanks[j].X = walls[i].X - walls[i].Width;
                                break;
                        }
                    }
                }
            }
            #endregion
            #region 判断玩家的子弹是否打到了墙体
            for(int i = 0; i < playerBullets.Count; i++)
            {
                for(int j = 0; j < walls.Count; j++)
                {
                    if (playerBullets[i].GetRectangle().IntersectsWith(walls[j].GetRectangle()))
                    {
                        playerBullets.Remove(playerBullets[i]);
                        walls.Remove(walls[j]);
                        break;
                    }
                }
            }
            #endregion
            #region 判断玩家的子弹是否和敌人的子弹相撞
            for(int i = 0; i < playerBullets.Count; i++)
            {
                for(int j = 0; j < enemyBullets.Count; j++)
                {
                    if (playerBullets[i].GetRectangle().IntersectsWith(enemyBullets[j].GetRectangle()))
                    {
                        playerBullets.Remove(playerBullets[i]);
                        enemyBullets.Remove(enemyBullets[j]);
                        break;
                    }
                }
            }
            #endregion
            #region 判断玩家坦克与敌人坦克是否相撞
            for(int i = 0; i < enemyTanks.Count; i++)
            {
                if (PT.GetRectangle().IntersectsWith(enemyTanks[i].GetRectangle()))
                {
                    enemyTanks[i].Life = 0;
                    enemyTanks[i].IsOver();
                }
            }
            #endregion
        }
        private void FireJudge(int type)
        {
            switch (type)
            {
                case 0://吃到五角星对玩家坦克进行加速
                    if (PT.FireLevel < 2) PT.FireLevel++;
                    break;
                case 1://吃到地雷清空屏幕所有敌人坦克
                    for(int i = 0; i < enemyTanks.Count; i++)
                    {
                        enemyTanks[i].Life = 0;
                        enemyTanks[i].IsOver();
                    }
                    break;
                case 2:
                    for(int i = 0; i < enemyTanks.Count; i++)
                    {
                        enemyTanks[i].IsStop = false;
                    }
                    break;
            }
        }
        public void RemoveObject(GameObject g)
        {
            if(g is PlayerBullet)
            {
                playerBullets.Remove(g as PlayerBullet);
            }
            if(g is Boom)
            {
                booms.Remove(g as Boom);
            }
            if(g is EnemyTank)
            {
                enemyTanks.Remove(g as EnemyTank);
            }
            if(g is EnemyBullet)
            {
                enemyBullets.Remove(g as EnemyBullet);
            }
            if(g is Born)
            {
                borns.Remove(g as Born);
            }
            if(g is Equipment)
            {
                equipments.Remove(g as Equipment);
            }
            if(g is Wall)
            {
                walls.Remove(g as Wall);
            }
        }
    }
}
